from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication, QMessageBox, QPushButton, QVBoxLayout, QHBoxLayout, QWidget, QLabel, QLineEdit
import sys
import threading

from scripts.test_script import TestCompent

class CustomWidget(QWidget):
    def __init__(self) -> None:
        self.test_compent = TestCompent()
        self.thread_test_script = None
        super().__init__()
        self.setWindowTitle('QLabel and  QLineEdit')

        # 设置 QLabel & QLineEdit
        label_01 = QLabel('账号：')
        self.line_edit_01 = QLineEdit()

        # 文本框可绑定事件
        #   文本内容变更，无参
        self.line_edit_01.textChanged.connect(self.textChanged)     
        #   光标位置变动，有两个可选参数，oldPos & newPos（还有其他的光标位置变化事件）
        self.line_edit_01.cursorPositionChanged.connect(self.cursorPositionChanged)   
        #   编辑完成事件，在"该单元格"按下Enter键，或者失去光标（且输入内容有变）触发
        self.line_edit_01.editingFinished.connect(self.editingFinished) 
        #   "只有"在"该单元格"按下Enter键触发（不推荐在文本框处使用）    
        self.line_edit_01.returnPressed.connect(self.returnPressed)   
        #   选择文本变化后触发， 按TAB      
        self.line_edit_01.selectionChanged.connect(self.selectionChanged)   
        #   文本内容变动，有一个参数：newVal
        self.line_edit_01.textEdited.connect(self.textEdited)       

        # 使用水平盒子（QHBoxLayout），将 QLabel & QLineEdit 组合到一起
        label_box_01 = QHBoxLayout()
        label_box_01.addWidget(label_01)
        label_box_01.addWidget(self.line_edit_01)
        

        label_02 = QLabel('密码：')
        self.line_edit_02 = QLineEdit()
        self.line_edit_02.setEchoMode(QLineEdit.Password)
        # 完成输入后，才会变成密码样式
        # self.line_edit_02.setEchoMode(QLineEdit.PasswordEchoOnEdit)

        label_box_02 = QHBoxLayout()
        label_box_02.addWidget(label_02)
        label_box_02.addWidget(self.line_edit_02)

        btn = QPushButton('登 陆')
        self.text_holder_label = QLabel('普通的消息提示文本Label')  
        # 设置文本样式为居右
        self.text_holder_label.setAlignment(Qt.AlignmentFlag.AlignRight)
        btn.clicked.connect(self.submit)

        # 设置整体布局为垂直布局
        layout = QVBoxLayout()
        layout.addLayout(label_box_01)    # 添加box的时候，需要使用 addLayout() , addItem() 好像也可以
        layout.addLayout(label_box_02)
        layout.addWidget(btn)           # 添加组件的时候，需要使用 addWidget()
        layout.addWidget(self.text_holder_label)
        self.setLayout(layout)

    def submit(self):
        print(f'用户名：{self.line_edit_01.text()}, 密码：{self.line_edit_02.text()}')
        self.text_holder_label.setText(f'你好 {self.line_edit_01.text()}')
        if self.thread_test_script and self.thread_test_script.is_alive():
            self.test_compent.toggleRun()
        else:
            self.thread_test_script = threading.Thread(target=self.test_compent.toggleRun)
            self.thread_test_script.start()
        
    def textChanged(self):
        self.text_holder_label.setText(f' textChanged: {self.line_edit_01.text()}')

    def cursorPositionChanged(self, old_pos, new_pos):
        print(f'cursorPositionChanged, old_pos: {old_pos}, new_pos: {new_pos}')
    
    def editingFinished(self):
        print('触发 editingFinished 事件')
    
    def returnPressed(self):
        print('触发 returnPressed 事件')
    
    def selectionChanged(self):
        print(f'触发 selectionChanged 事件, 选中文本内容：{self.line_edit_01.selectedText()}')
        print('触发 returnPressed 事件')
    
    def textEdited(self, new_val):
        print(f'触发 textEdited 事件, new_val: {new_val}')


app = QApplication(sys.argv)


window = CustomWidget()
window.show()

app.exec()
